﻿//////////////////////////////////////////////////////////
// Project : Exam notifier
// Group : Coc Dam Code
// Team : Uploader
// Author : ToanNDSE60905@fpt.edu.vn
// Date : 11/11/2011
//////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StreamWithProgress;

namespace AppClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void MsgStatus(string txtStr)
        {
            StatusLabel1.Text = txtStr;
        }
        private bool Uploader(string txtStr)
    {
        try
        {
            String fileName = txtStr;
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName);
            using (System.IO.FileStream stream = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                using (StreamProgress upLoadStreamWithProcess = new StreamProgress(stream))
                {

                    // start service client
                    FileTransferService.FileTransferServiceClient client = new FileTransferService.FileTransferServiceClient();

                    // upload file
                    
                    client.UploadFile(fileInfo.Name, fileInfo.Length, upLoadStreamWithProcess);
                    
                    client.Close();
                    return true;
                }
            }
        }
        catch (System.ServiceModel.EndpointNotFoundException)
        {
            MsgStatus ( "Kết nối máy chủ thất bại. Xin vui lòng thử lại");
            return false;
        }
    }
        private void LogMsg(string aMsg)
        {
            string Line = null;
            if (txtLog.Text.CompareTo("") > 0)
            {
                Line = "\r" + "\n";
            }
            Line = Line + "[" + DateTime.Now.ToString("hh:mm:ss") + "]: ";
            Line = Line + aMsg;
            txtLog.SelectionStart = txtLog.Text.Length;
            txtLog.SelectedText = Line;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string Chosen_File;
            OpenDlg.Title = "Lựa chọn file Excel để Upload";
            OpenDlg.Multiselect = true;
            OpenDlg.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            OpenDlg.Filter = "Excel File XLS|*.xls";
            if (OpenDlg.ShowDialog() != DialogResult.Cancel)
            {
                int i = 0;
                int n_File = OpenDlg.FileNames.Count();
                LogMsg("Tổng số file Upload : " + n_File.ToString());
                while (i<n_File)
                {
                    //MessageBox.Show(OpenDlg.FileNames[i]);
                    MsgStatus("Bắt đầu Upload File");
                    LogMsg("Bắt đầu Upload File " + OpenDlg.FileNames[i]);
                    if (Uploader(OpenDlg.FileNames[i]))
                    {
                        LogMsg("Upload thành công File " + OpenDlg.FileNames[i]);
                    }
                    else
                    {
                        LogMsg("Upload thất bại File " + OpenDlg.FileNames[i]);
                    }
                    
                    i++;
                }
                
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            txtLog.Text  = "";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LogMsg("Dịch vụ sẵn sàng phục vụ");
        }
    }
}
